<?php

class Tpl {

    static public function offset($rowsPerPage) {
        $pageNum = isset($_GET['p']) ? intval($_GET['p']) : 1;
        if ($pageNum < 1) {
            $pageNum = 1;
        }
        return ($pageNum - 1) * $rowsPerPage;
    }

    static public function page($totalRow, $rowsPerPage, $url, $displayMode = 1, $pageRange = 0) {
        $currentPage = isset($_GET['p']) ? intval($_GET['p']) : 1;
        $totalPages = 0 == $rowsPerPage ? 0 : ceil($totalRow / $rowsPerPage);

        if (0 == $totalPages) {
            return;
        }

        if ($currentPage < 1) {
            $currentPage = 1;
        }

        if ($currentPage > $totalPages) {
            $currentPage = $totalPages;
        }

        ob_start();
        switch ($displayMode) {
            case 2:

                $start = 1;

                if ($currentPage > $pageRange - 1) {
                    $start = floor($currentPage / $pageRange) * $pageRange;
                }

                if ($start > 1) {
                    echo ' <a href="', $url, 1, '">First</a> ';
                }

                if ($currentPage > 1)
                    echo "<a href='" . $url . ($currentPage - 1) . "' title='" . fl('p_previous') . "'><b>&larr; &nbsp;</b></a> ";

                $i = $start;

                while ($i <= $start + $pageRange - 1 && $i <= $totalPages) {
                    if ($i == $currentPage)
                        echo " <b>[$i]</b> ";
                    else
                        echo " <a href='", $url, $i, "'>[$i]</a> ";
                    $i++;
                }

                if ($currentPage < $totalPages) {
                    echo " <b><a href='$url", ($currentPage + 1), "'>&rarr;</a></b> ";
                }

                if ($start + $pageRange - 1 < $totalPages) {
                    echo ' <a href="', $url, $totalPages, '">Last</a> ';
                }

                if ($currentPage > $pageRange || $totalPages > $pageRange)
                    echo '<br />';

                if ($currentPage > $pageRange) {
                    echo " <a href='$url", ($currentPage - $pageRange), "'><b>&lt;&lt; $pageRange ", fl('p_previous'), "</b></a> | ";
                }

                if ($totalPages > $pageRange * ceil($currentPage / $pageRange)) {
                    echo " <a href='$url", ((floor($currentPage / $pageRange) * $pageRange) + $pageRange + 1), "'><b>$pageRange ", fl('p_next'), "&gt;&gt;</b></a>";
                }

                break;

            case 3:

                $firstPage = $currentPage - $pageRange;
                $lastPage = $currentPage + $pageRange;

                if ($firstPage < 1) {
                    $firstPage = 1;
                }

                if ($lastPage > $totalPages) {
                    $lastPage = $totalPages;
                }

                if ($firstPage > 1) {
                    echo ' <a href="', $url, 1, '">', fl('p_first'), '</a> ';
                }

                if ($currentPage > 1) {
                    echo ' <a href="', $url, ($currentPage - 1), '">', fl('p_previous'), '</a>';
                }

                for ($pageCounter = $firstPage; $pageCounter <= $lastPage; ++$pageCounter) {
                    if ($pageCounter === $currentPage) {
                        echo ' <span>', $pageCounter, '</span> ';
                    } else {
                        echo ' <a href="', $url, $pageCounter, '">', $pageCounter, '</a>';
                    }
                }

                if ($currentPage < $totalPages) {
                    echo ' <a class="next_link" href="', $url, $currentPage + 1, '">', fl('p_next'), '</a>';
                }

                if ($lastPage < $totalPages) {
                    echo ' <a href="', $url, $totalPages, '">', fl('p_last'), '</a> ';
                }

                break;

            case 1:
            default:
                if ($totalPages > 1) {
                    if ($currentPage > 1) {
                        echo '<div class="prev_link_activ"> <a href="', $url, ($currentPage - 1), '">', 'Prev Page', '</a></div>';
                    } else {
                        echo '<div class="prev_link_inactiv"> <a>', 'Prev Page', '</a></div>';
                    }

                    echo '<div class="middle_link">';
                    for ($counter = 1; $counter <= $totalPages; ++$counter) {
                        if ($totalPages > 1) {
                            if ($counter === $currentPage) {
                                echo '<span><a class="active">', $counter, '</a></span>';
                            } else {
                                echo '<span><a href="', $url, $counter, '">', $counter, '</a></span>';
                            }
                        }
                    }
                    echo '</div>';

                    if ($currentPage < $totalPages) {
                        echo '<div class="next_link_activ"> <a href="', $url, ($currentPage + 1), '">', 'Next Page', '</a></div>';
                    } else {
                        echo '<div class="next_link_inactiv"> <a>', 'Next Page', '</a></div>';
                    }
                }
                break;
        }
        return ob_get_clean();
    }

    public static function setMenuMode($page) {
        $basePage = '| <a href="useradmin/mode/edit?id=' . $_SESSION['a_id'] . '"> ' . fl('lt_myaccount') . '</a> | <a href="help?g=' . $_SESSION['a_group'] . '"> ' . fl('lt_help') . '</a>';
        if ($_SESSION['a_group'] == 1) {
            $menucontent = '<a href="home"> ' . fl('lt_home') . ' </a> &nbsp;  |<a href="' . $page . '"> ' . fl('lt_list') . '<b></b></a> |<a href="' . $page . '/mode/edit"> ' . fl('lt_add') . '</a> ';
        } elseif ($_SESSION['a_group'] == 5 && $_SESSION['a_access'] > 1) {
            $menucontent = '<a href="' . $page . '"> ' . fl('lt_home') . ' </a> &nbsp;  |<a href="' . $page . '/mode/edit"> ' . fl('lt_add') . '</a> ';
        } else {
            $menucontent = '<a href="' . $page . '"> ' . fl('lt_home') . ' </a> &nbsp;  ';
        }
        return $menucontent . $basePage;
    }

    public static function showCountryLists($selectedID, $style) {
        $result = Http::SOAPClient()->getListCountries();
        $countryList = array();
        $countryList = $result->getListCountriesResult->Countries;

        echo '<select name="country" id="form" class="<?php echo $style; ?>"><option value="0">(Country)</option>';
        foreach ($countryList as $country):
            if ($selectedID == $country->CountryID) {
                //echo $country->LongName;
                echo '<option value="' . $country->CountryID . '" selected="selected">' . $country->LongName . '</option>';
            }
            else
                echo '<option value="' . $country->CountryID . '">' . $country->LongName . '</option>';
        endforeach;
        echo '</select>';
    }

    public static function showCountryList($selectedID) {
        showCountryLists($selectedID, 'txt');
    }

}
